<?php
class Admin_FeedbackController extends Zend_Controller_Action {
	private $_db;
	
	public function init() {
		/* Initialize action controller here */
		$this->_db = new Application_Model_DbTable_Feedback();
	}
	public function indexAction() {
		$this->view->feedbacks = $this->_db->fetchAll('del_flag = 0');		
		
	}
	
	public function viewAction(){
		$id = $this->getParam("id");
		$feedback = $this->_db->find($id) ->current();
		$where = $this->_db->getAdapter()->quoteInto('id = ?', $id);
		$this->_db->update(array('status' => 1), $where);
		$this->view->feedback = $feedback;
	}
	
	public function replyAction(){
		if ($this->getRequest ()->isPost ()) {
			try {
				//send mail
				$config = array(
						'ssl' => 'tls',
						'port' => 587,
						'auth' => 'login',
						'username' => 'phonesale.ecom@gmail.com',
						'password' => 'asd654321'
				);
				$transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);
				Zend_Mail::setDefaultTransport($transport);
		
				$mail = new Zend_Mail ( 'UTF-8' );
				$mail->setBodyHtml ($this->getParam('replyContent') );
				$mail->addTo ( $this->getParam('email') );
				$mail->setSubject ( 'Trả lời feedback: ' . $this->getParam('title') );
				$mail->setBodyHtml($this->getParam('replyContent'));
				$mail->setFrom ( 'phonesale.ecom@gmail.com');
			
				$mail->send ();
				$id = (int) $this->getParam("id");
				$feedback = $this->_db->find($id) ->current();
				$where = $this->_db->getAdapter()->quoteInto('id = ?', $id);
				$this->_db->update(array('status' => 2), $where);
			} catch (Exception $ex) {
				echo $ex->getMessage();
				$this->redirect ( "/admin/feedback" );
			}
	
			
		}
		$this->redirect ( "/admin/feedback" );
	}
	
	public function deleteAction(){
		$id = (int) $this->getParam("id");
		$where = $this->_db->getAdapter()->quoteInto('id = ?', $id);
		$this->_db->update(array('del_flag' => 1), $where);
		$this->redirect ( "/admin/feedback" );
	}
}

